package com.hellobike.haxing.modules.supplier.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierClassificationService;
import com.hellobike.haxing.modules.supplier.service.ITSupplierContractService;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.supplier.util.Convert;
import com.hellobike.haxing.modules.system.service.ISysUserService;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: t_supplier_info
 * @Author: haxing-boot
 * @Date: 2020-01-13
 * @Version: V1.0
 */
@RestController
@RequestMapping("/supplierInfo/tSupplierInfo")
@Slf4j
public class TSupplierInfoController extends BaseController<TSupplierInfo, ITSupplierInfoService> {
    @Autowired
    private ITSupplierInfoService tSupplierInfoService;
    @Autowired
    private ITSupplierContractService tSupplierContractService;
    @Autowired
    private ITSupplierClassificationService tSupplierClassificationService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param tSupplierInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "t_supplier_info-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(TSupplierInfo tSupplierInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<TSupplierInfo> queryWrapper = QueryGenerator.initQueryWrapper(tSupplierInfo, req.getParameterMap());
        Page<TSupplierInfo> page = new Page<TSupplierInfo>(pageNo, pageSize);
        IPage<TSupplierInfo> pageList = tSupplierInfoService.page(page, queryWrapper);
        if (pageList.getTotal() > 0) {
            pageList.getRecords().forEach(
                    item -> {
                        List<String> sc = Convert.json2list(item.getSupplyCategory());
                        item.setSupplyCategorys(sc);
                        if (ListUtil.isNotEmpty(sc)) {
                            item.setSupplyCategoryStr(String.join(",", sc));
                        }
                    }
            );
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param tSupplierInfo
     * @return
     */
    @AutoLog(value = "t_supplier_info-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody TSupplierInfo tSupplierInfo) {
        return tSupplierInfoService.saveSupplier(tSupplierInfo);
//
//        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username", tSupplierInfo.getMobile());
//        SysUser sysUser = sysUserService.getOne(queryWrapper);
//        if (sysUser != null) {
//            return Result.error("用户名/手机号已存在！");
//        }
//
//        QueryWrapper<TSupplierInfo> query = new QueryWrapper<>();
//        query.eq("mobile", tSupplierInfo.getMobile());
//        TSupplierInfo tSupplierUser = tSupplierInfoService.getOne(query);
//        if (tSupplierUser != null) {
//            return Result.error("用户名/手机号已存在！");
//        }
//
//        tSupplierInfo.setDeleted(0);
//        tSupplierInfo.setSupplyCategory(Convert.list2Json(tSupplierInfo.getSupplyCategorys()));
//        tSupplierInfo.setState(Constant.INFO_IS_PASS);
//        tSupplierInfoService.save(tSupplierInfo);
//        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param tSupplierInfo
     * @return
     */
    @AutoLog(value = "t_supplier_info-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TSupplierInfo tSupplierInfo) {
        return this.tSupplierInfoService.editSupplier(tSupplierInfo);
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "t_supplier_info-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        tSupplierInfoService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "t_supplier_info-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.tSupplierInfoService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询应商信息
     *
     * @param json
     * @return
     */
    @PostMapping(value = "/detail")
    public Result<?> detail(@RequestBody JSONObject json) {
        Long headerId = json.getLong("headerId");
        TSupplierInfo tSupplierInfo = tSupplierInfoService.getById(headerId);
        return Result.ok(tSupplierInfo);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        TSupplierInfo tSupplierInfo = tSupplierInfoService.getById(id);
        if (tSupplierInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(tSupplierInfo);
    }

    /**
     * 暂停
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/pause")
    public Result<?> pause(@RequestParam(name = "id", required = true) Long id) {
        TSupplierInfo tSupplierInfo = new TSupplierInfo();
        tSupplierInfo.setId(id);
        tSupplierInfo.setState(3);
        tSupplierInfoService.updateById(tSupplierInfo);
        return Result.ok("已暂停");
    }

    /**
     * 启用
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/resume")
    public Result<?> resume(@RequestParam(name = "id", required = true) Long id) {
        TSupplierInfo tSupplierInfo = new TSupplierInfo();
        tSupplierInfo.setState(4);
        tSupplierInfo.setId(id);
        tSupplierInfoService.updateById(tSupplierInfo);
        return Result.ok("已启用");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param tSupplierInfo
     */
    @AutoLog(value = "t_supplier_info-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TSupplierInfo tSupplierInfo) {
        return super.exportXls(request, tSupplierInfo, TSupplierInfo.class, "t_supplier_info");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "t_supplier_info-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TSupplierInfo.class);
    }

}
